package com.feihong.txgw.ui.activity.order;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.feihong.txgw.R;
import com.feihong.txgw.adapter.ReViewOrderAdapter;
import com.feihong.txgw.inter.contract.order.IReviewOrderContract;
import com.feihong.txgw.inter.listener.order.OnReviewAnoymityListener;
import com.feihong.txgw.inter.listener.order.OnReviewDestribeSelectListener;
import com.feihong.txgw.inter.listener.order.OnReviewEidtChangeListener;
import com.feihong.txgw.inter.listener.order.OnReviewFlowSelectListener;
import com.feihong.txgw.inter.listener.order.OnReviewServiceSelectListener;
import com.feihong.txgw.json.user.ProductOrderDetailEntity;
import com.feihong.txgw.message.LoginMessage;
import com.feihong.txgw.message.RefreshOrder;
import com.feihong.txgw.tx_precenter.ReviewMainPrecenter;
import com.feihong.txgw.ui.mvp.BaseMVPCompateActivity;
import com.feihong.txgw.ui.mvp.precenter.AbBasePrecenter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author wangziheng
 * @name txgw_app
 * @class name：com.feihong.txgw.ui.activity.order
 * @class describe: 订单评价
 * @time 2017/8/10 16:01
 * @change
 * @chang time
 * @class describe
 */
public class ReViewOrderActivity extends BaseMVPCompateActivity<IReviewOrderContract.AbReviewOrderMainPrecenter> implements IReviewOrderContract.IReviewOrderMainView {
    private TextView mSubmit;
    @BindView(R.id.top_back_layout)
    RelativeLayout mTitleLayout;
    private RelativeLayout.LayoutParams mTitleParams;
    /**
     * 订单号  订单类型
     */
    private String orderSn;
    private String orderType;
    private Intent mIntent;
    private String mFlowRatingStar = "0";
    private String mServiceRatingStar = "0";
    private String isAnoymityStr = "1";

    private String order_id;
    private int type;
    /**
     * 默认0  1  从订单详情跳转而来  评论成功之后  需要刷新订单详情亦或是 刷新订单列表
     */
    private int trueRresh = 0;

    @BindView(R.id.review_order_goods_recyclerView)
    RecyclerView mRecycler;
    private LinearLayoutManager linearLayoutManager;
    private ReViewOrderAdapter mAdapter;
    @BindView(R.id.lay_loading)
    RelativeLayout mLoadingLayout;
    @BindView(R.id.connect_net_error_layout)
    LinearLayout mConnectErrorLayout;
    @BindView(R.id.re_query_data_layout)
    LinearLayout mReConnectLayout;

    @Override
    public void setTheme() {

    }

    @Override
    public int setLayoutID() {
        return R.layout.activity_review_order;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setBackButtonFinish();
        setPageTitle("评价");
        mIntent = getIntent();

        order_id = mIntent.getStringExtra("order_id");
        orderSn = mIntent.getStringExtra(PayActivity.INTENT_ORDER_ID);
        type = mIntent.getIntExtra(PayActivity.INTENT_PAY_TYPE, 0);
        trueRresh = mIntent.getIntExtra("trueRefresh", 0);
        if (type == PayActivity.PAY_TYPE_PRODUCT) {
            orderType = "1";
        } else if (type == PayActivity.PAY_TYPE_KAIKA) {
            orderType = "3";
        } else if (type == PayActivity.PAY_TYPE_PHONE_GPRS || type == PayActivity.PAY_TYPE_PHONE_RECHARGE) {
            orderType = "2";
        }

        mSubmit = new TextView(this);
        mSubmit.setText("发布");
        mTitleLayout.addView(mSubmit);
        mTitleParams = new RelativeLayout.LayoutParams(dip2px(this, 70), ViewGroup.LayoutParams.MATCH_PARENT);
        mTitleParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        mSubmit.setPadding(dip2px(this, 10), 0, dip2px(this, 15), 0);
        mSubmit.setGravity(Gravity.CENTER);
        mSubmit.setTextColor(getResources().getColor(R.color.appRed));
        mSubmit.setLayoutParams(mTitleParams);
        mSubmit.setVisibility(View.GONE);
        linearLayoutManager = new LinearLayoutManager(this);
        mAdapter = new ReViewOrderAdapter(this);
        mRecycler.setLayoutManager(linearLayoutManager);
        mRecycler.setAdapter(mAdapter);

        mRecycler.setVisibility(View.GONE);
        mLoadingLayout.setVisibility(View.VISIBLE);
        mConnectErrorLayout.setVisibility(View.GONE);
        mReConnectLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mConnectErrorLayout.setVisibility(View.GONE);
                mLoadingLayout.setVisibility(View.VISIBLE);
                getOrderGoodsInfo();
            }
        });
    }

    @Override
    public void bindData() {

        mSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submitReView();
            }
        });

        getOrderGoodsInfo();

        mAdapter.setOnAnyomityListener(new OnReviewAnoymityListener() {
            @Override
            public void setOnAnoymitySelect(int positoin, boolean isAnoymity) {
                if (isAnoymity) {
                    isAnoymityStr = "1";
                } else {
                    isAnoymityStr = "0";
                }
                mReviewOrderData.get(positoin).setIs_anonymous(isAnoymityStr);
            }
        });

        mAdapter.setOnFlowSelectListener(new OnReviewFlowSelectListener() {
            @Override
            public void setOnReviewFlowSelectListener(int starNum) {
                mFlowRatingStar = String.valueOf(starNum);
            }
        });

        mAdapter.setOnReviewEditChangeLisetener(new OnReviewEidtChangeListener() {
            @Override
            public void setOnReviewEditChangetText(int positoin, String editStr) {
                mReviewOrderData.get(positoin).setComment(editStr);
            }
        });

        mAdapter.setOnReviewDestribeSelectListener(new OnReviewDestribeSelectListener() {
            @Override
            public void setOnReviewDestribeSelectListtener(int position, int startNum) {
                if (startNum == 0) {
                    showDialog(null, "商品评论不能为0星");
                    return;
                }
                mReviewOrderData.get(position).setGoods_rand(String.valueOf(startNum));
            }
        });

        mAdapter.setOnServiceSelectListener(new OnReviewServiceSelectListener() {
            @Override
            public void setOnReviewSerivceSelectListener(int startNum) {
                mServiceRatingStar = String.valueOf(startNum);
            }
        });

    }

    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

    private void submitReView() {
        if ("0".equals(mFlowRatingStar)) {
            showDialog(null, "物流评分不能为0星");
            return;
        }
        if ("0".equals(mServiceRatingStar)) {
            showDialog(null, "服务评分不能为0星");
            return;
        }
        showProgressDialog("评论提交中");
        mPresenter.submitOrderReview(orderSn,orderType,mFlowRatingStar,mServiceRatingStar,JSON.toJSONString(mReviewOrderData));
    }

    /**
     * 点击任意地方 隐藏弹出框
     */

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private List<ProductOrderDetailEntity> mDetailList = new ArrayList<ProductOrderDetailEntity>();
    private List<ReViewOrderEntity> mReviewOrderData = new ArrayList<>();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissProgressDialog();
    }

    public void getOrderGoodsInfo() {
        mPresenter.loadOrderGoodsInfo(String.valueOf(type), order_id);
    }

    @Override
    public void getOrderGoodsError() {
        if (activityIsDestory()) {
            return;
        }
        mLoadingLayout.setVisibility(View.GONE);
        mRecycler.setVisibility(View.GONE);
        mConnectErrorLayout.setVisibility(View.VISIBLE);
    }

    @Override
    public void getOrderGoodsResult(List<ProductOrderDetailEntity> orderGoods) {
        if (activityIsDestory()) {
            return;
        }
        mSubmit.setVisibility(View.VISIBLE);
        mLoadingLayout.setVisibility(View.GONE);
        mConnectErrorLayout.setVisibility(View.GONE);
        mAdapter.setData(mDetailList);
        for (int i = 0; i < mDetailList.size(); i++) {
            ReViewOrderEntity reviewOrder = new ReViewOrderEntity();
            reviewOrder.setComment("");
            reviewOrder.setGoods_rand("5");
            reviewOrder.setIs_anonymous("1");
            reviewOrder.setSku_id(mDetailList.get(i).getSku_id());
            mReviewOrderData.add(reviewOrder);
        }
    }

    @Override
    public void submitOrderReviewResult() {
        dismissProgressDialog();
        mLoadingLayout.setVisibility(View.GONE);
        if(trueRresh == 0){
            EventBus.getDefault().post(new RefreshOrder());
        }
        showDialog(null, "评论成功", new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (trueRresh == 0) {
                    finish();
                } else {
                    Intent intent = new Intent(ReViewOrderActivity.this, MyOrderActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

    @Override
    public void submitOrderReviewError() {

        dismissProgressDialog();
    }

    @Override
    public void loadDataComplated() {

        dismissProgressDialog();
    }

    @Override
    public void loadDataError() {
        if(activityIsDestory()){
            return;
        }
        mLoadingLayout.setVisibility(View.GONE);
        mRecycler.setVisibility(View.GONE);
        mConnectErrorLayout.setVisibility(View.VISIBLE);
    }

    @NonNull
    @Override
    public AbBasePrecenter initPresenter() {
        return ReviewMainPrecenter.create();
    }

    class ReViewOrderEntity {
        /**
         * 商品评级
         */
        private String goods_rand;
        /**
         * 是否匿名
         */
        private String is_anonymous;
        /**
         * 评论内容
         */
        private String comment;
        private String sku_id;

        public ReViewOrderEntity() {
        }

        public String getSku_id() {
            return sku_id;
        }

        public void setSku_id(String sku_id) {
            this.sku_id = sku_id;
        }

        public String getGoods_rand() {
            return goods_rand;
        }

        public void setGoods_rand(String goods_rand) {
            this.goods_rand = goods_rand;
        }

        public String getIs_anonymous() {
            return is_anonymous;
        }

        public void setIs_anonymous(String is_anonymous) {
            this.is_anonymous = is_anonymous;
        }

        public String getComment() {
            return comment;
        }

        public void setComment(String comment) {
            this.comment = comment;
        }
    }

}
